<?php
class Home extends Front_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('knowledgegroup/knowledgegroup_model','knowledgegroup');
		$this->load->model('division/division_model','division');
		$this->load->model('section/section_model','section');
		$this->load->model('users/users_model','users');
		$this->load->model('admin_notify/notify_model','notify');
	}
	
	public function index()
	{
		$data='';
		$data['knowledegroup'] = $this->knowledgegroup->limit(50)->get();
		$data['division'] = $this->division->limit(50)->get();
		$data['section'] = $this->section->limit(50)->get();
		$this->template->build('index',$data);
	}
	
	
	public function login()
	{
		$status = login($_POST['username'],$_POST['password']);
		if($status==1)
		{			
			redirect('home');
		}
		else
		{
			redirect('home');
		}
	}
		public function signout(){		
		logout();
		redirect('home');
	}
		
	public function register()
	{
		$this->template->build('register');
	}
	
	public function save()
	{
		$_POST['password'] = $_POST['password']=='' ? $_POST['current_password'] : $_POST['password'];
		$_POST['registerdate'] = date("Y-m-d H:i:s");
		$this->users->save($_POST);	
		redirect('home');
	}
	
	public function forgot_password()
	{
		
		if(@$_POST['username']!='' && $_POST['email']!=''){
			
			$password = $this->db->getone("select password from user where username =".$_POST['username']);			
			/*
			$strTo = $_POST['email'];
			$strSubject = "แจ้งรหัสผ่านการเข้าใช้งานระบบคลังความรู้สนับสนุนสาธารณสุข";
			$strHeader = "webmaster@ph.mahidol.ac.th";
			*/
			$m = "<p><b>แจ้งรหัสผ่านการเข้าใช้งานระบบคลังความรู้สนับสนุนสาธารณสุข</b></p>";
			$m .= "<p>Username : ".$_POST['username']."</p>";
			$m .= "<p>Password : ".$password."</p>";
			$m .= "<p><b></b></p>";
			//mail($strTo,$strSubject,$m,$strHeader);
			
							
			require_once('include/PHPMailer/class.phpmailer.php');			
			$subject = "แจ้งรหัสผ่านการเข้าใช้งานระบบคลังความรู้สนับสนุนสาธารณสุข";

			$mail             = new PHPMailer(); // defaults to using php "mail()"				
			$body             = $m;	
			//$body             = eregi_replace("[\]",'',$body);
			$mail->AddReplyTo("webmaster@ph.mahidol.ac.th", 'webmaster@ph.mahidol.ac.th');			
			$mail->SetFrom("webmaster@ph.mahidol.ac.th", 'webmaster@ph.mahidol.ac.th');		
			$mail->AddAddress( $_POST['email'], "");				
			
			
			$mail->Subject    = $subject;	
			$mail->CharSet = "utf-8";
			$mail->MsgHTML($body);		
			if(!$mail->Send()) {
			//  echo "Mailer Error: " . $mail->ErrorInfo;
			} else {
			//  echo "Message sent!";
			}			
		}
		$this->template->build('forgot_password');
	}
	
	public function check_email(){
		
		$emails=trim($_GET["email"]);
		$chk_email = $this->db->getone("select count(*) from users where email ='".$emails."'");
		if($chk_email > 0)
		{			
			echo 'false';	
		}
		else
		{
			echo 'true';
		}
	}
	
	public function check_username(){
		
		$username=trim($_GET["username"]);
		$chk_username = $this->db->getone("select count(*) from users where username ='".$username."'");
		if($chk_username > 0)
		{			
			echo 'false';	
		}
		else
		{
			echo 'true';
		}
	}
	public function notify($id=FALSE)
	{
		if(!empty($id)){
		$data['notify'] = $this->notify->where("id=".$id)->get();
		$this->load->view('notify',$data);
		$this->template->build('notify');
		}
	}

}
?>